package fr.jade.service.fm.deployment;

import java.util.Map;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import fr.jade.base.JadeBaseObject;
import fr.jade.fraclite.api.control.GenericAttributeController;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.util.FractalUtil;

public class SetAttributeTask extends JadeBaseObject implements DeploymentTask {

  protected NewComponentTask cmpTask;
  protected Map<String, String> attributes;
  
  public SetAttributeTask(NewComponentTask ct, Map<String, String> attributes){
    assert ct != null;
    assert attributes != null;
    
    this.cmpTask=ct;
    this.attributes=attributes;
  }
  
  public void execute() throws DeploymentException {
    log("execute :" +toString());
    Component cmp = cmpTask.getComponent();
    try{
    GenericAttributeController gac = FractalUtil.getAttributeController(cmp);
    for(Map.Entry<String, String> entry : attributes.entrySet())
      gac.setAttribute(entry.getKey(), entry.getValue());
    }catch(NoSuchInterfaceException e){
      throw new DeploymentException("Attribute task error", e);
    } catch (NoSuchAttributeException e) {
      throw new DeploymentException("Attribute task error", e);
    }
  }

  public String getLevelName(){
    return "setAttribute";
  }
  
  public String toString(){
    return "SetAttributeTask["+ cmpTask.toString() + " - " + attributes.toString() +"]";
  }
}
